package com.itheima.health.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.mapper.UserMapper;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import com.itheima.health.vo.RoleVO;
import com.itheima.health.vo.UserVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    //根据账号查询当用户角色权限信息
    @Override
    public UserVO findUserDetailsByUsername(String username) {
        //根据查询用户名称查询用户id
        UserVO userVO = baseMapper.findUserByUsername(username);
        //获取用户的id
        Integer uId = userVO.getId();
        //根据用户id查询用户对应的角色
        List<RoleVO> roleVOList = baseMapper.findRolesByUserId(uId);
        for (RoleVO roleVO : roleVOList) {
            //获取角色id
            Integer rid = roleVO.getId();
            //根据角色id查询查询角色权限
            List<Permission> permissionList = baseMapper.findPermissionByRoleId(rid);
            //将权限信息放入角色中
            roleVO.setPermissionList(permissionList);
        }
        //将角色信息放入用户中
        userVO.setRoles(roleVOList);
        return userVO;
    }
}
